home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c
- Subject: Re: volatile type
- Date: 14 Feb 1996 02:37:03 -0500
- Organization: Montclair State University
- Message-ID: <harmon.824282241@pegasus.montclair.edu>
- References: <4fro3j$jbi@service.polymtl.ca>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- fermi@info.polymtl.ca (Yannick Heneault) writes:
-
- >Can someone know information about this data type. A example is
- >welcome!
-
- volatile isn't a data type, its a modifier to a data declaration, much
- like const, static, or extern communicate additional information about a
- particular data declaration to the compiler. In the case of the volatile
- keyword, it means that this element of data could be changed at any time
- by any means, including means outside the control of your own program
- (for example, other processes or interrupts). For this reason, the
- compiler is informed that it should always update its value of that
- element from memory, and not rely on more optimized handling that would
- place it in a register and assume that if it hasn't been referenced in
- the program, then it hasn't changed. volatile means that it could have
- changed, and there is no way for the compiler to know when or how, it
- must always fetch it when called upon.
-
- ex.
- volatile int widgets;
- :
- widgets = 0;
- :
- while ( widgets < 10 ) {
- printf("not enough widgets\n");
- }
-
- In this example, widgets are initialized to 0. We will assume somewhere
- that everytime a clock interrupt occurs, or an assembly line worker pulls a
- chord signifying a new widget has been produced, that widgets is modified by
- an external program (interrupt service routine, parent process, whatever)
- and incremented.
-
- Any reasonable compiler would see that the while loop is infinite, and as
- widgets is not changed within the loop, there would ordinarily be no reason
- for the compiler to generate code that, at the while condition, would check
- widgets against 10. After all, a shortened jump and tighter loop would
- ordinarily be considered an optimization, it would be more efficient. BUT,
- in this case we know that widgets could exceed 10 during the loop. Therefore
- widgets is declared as volatile. This forces the compiler to test the con-
- dition by comparing widgets to 10, and more so, it prevents the compiler from
- copying the value of widgets from out in memory into a high-speed register
- (since if widgets were updated since that copy were made, the register would
- not likewise be updated), instead it must consult the memory location for
- widgets.
-
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... CYCLIC REDUNDANCY CHECK - stocktaking at a bicycle shop.
-
-